<script lang="ts" setup>
import type { OnActionClickParams } from '#/adapter/vxe-table';
import type { BaseinfoProductApi } from '#/api/csyt/baseinfoproduct';

import { h, nextTick, watch } from 'vue';

import { Plus } from '@vben/icons';

import { Button, Input, RadioGroup, Select } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getBaseinfoLableinfoListByProductNo } from '#/api/csyt/baseinfoproduct';
import { $t } from '#/locales';

import { useBaseinfoLableinfoGridEditColumns } from '../data';

const props = defineProps<{
  productNo?: number; // 产品编码（主表的关联字段）
}>();

/** 表格操作按钮的回调函数 */
function onActionClick({
  code,
  row,
}: OnActionClickParams<BaseinfoProductApi.BaseinfoLableinfo>) {
  switch (code) {
    case 'delete': {
      onDelete(row);
      break;
    }
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useBaseinfoLableinfoGridEditColumns(onActionClick),
    border: true,
    showOverflow: true,
    autoResize: true,
    keepSource: true,
    rowConfig: {
      keyField: 'id',
    },
    pagerConfig: {
      enabled: false,
    },
    toolbarConfig: {
      enabled: false,
    },
  },
});

/** 删除标签信息 */
const onDelete = async (row: BaseinfoProductApi.BaseinfoLableinfo) => {
  await gridApi.grid.remove(row);
};

/** 提供获取表格数据的方法供父组件调用 */
defineExpose({
  getData: (): BaseinfoProductApi.BaseinfoLableinfo[] => {
    const data =
      gridApi.grid.getData() as BaseinfoProductApi.BaseinfoLableinfo[];
    const removeRecords =
      gridApi.grid.getRemoveRecords() as BaseinfoProductApi.BaseinfoLableinfo[];
    const insertRecords =
      gridApi.grid.getInsertRecords() as BaseinfoProductApi.BaseinfoLableinfo[];
    return data
      .filter((row) => !removeRecords.some((removed) => removed.id === row.id))
      .concat(insertRecords.map((row: any) => ({ ...row, id: undefined })));
  },
});

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.productNo,
  async (val) => {
    if (!val) {
      return;
    }
    await nextTick();
    await gridApi.grid.loadData(
      await getBaseinfoLableinfoListByProductNo(props.productNo!),
    );
  },
  { immediate: true },
);
</script>

<template>
  <Grid class="mx-4">
    <template #lableNo="{ row }">
      <Input v-model:value="row.lableNo" />
    </template>
    <template #lableName="{ row }">
      <Input v-model:value="row.lableName" />
    </template>
    <template #lableType="{ row, column }">
      <Select v-model:value="row.lableType" class="w-full">
        <Select.Option
          v-for="option in column.params.options"
          :key="option.value"
          :value="option.value"
        >
          {{ option.label }}
        </Select.Option>
      </Select>
    </template>
    <template #lableDescription="{ row }">
      <Input v-model:value="row.lableDescription" />
    </template>
    <template #lableTemp="{ row }">
      <Input v-model:value="row.lableTemp" />
    </template>
    <template #status="{ row, column }">
      <RadioGroup v-model:value="row.status" :options="column.params.options" />
    </template>
    <template #flag="{ row, column }">
      <RadioGroup v-model:value="row.flag" :options="column.params.options" />
    </template>
  </Grid>
</template>
